package com.yssw.news.service.impl;

import java.util.List;

import com.yssw.common.core.domain.entity.SysDictData;
import com.yssw.common.utils.DateUtils;
import com.yssw.model.vo.news.NewsTypeVo;
import com.yssw.model.vo.news.NewsVo;
import com.yssw.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yssw.news.mapper.NewsTableMapper;
import com.yssw.news.domain.NewsTable;
import com.yssw.news.service.INewsTableService;

/**
 * 新闻Service业务层处理
 *
 * @author yssw
 * @date 2023-03-27
 */
@Service
public class NewsTableServiceImpl implements INewsTableService {


    @Autowired
    private NewsTableMapper newsTableMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询新闻
     *
     * @param id 新闻主键
     * @return 新闻
     */
    @Override
    public NewsVo selectNewsTableById(Long id) {
//        NewsVo newsVo = newsTableMapper.selectNewsTableById(id);
        return newsTableMapper.selectNewsTableById(id);
    }

    /**
     * 查询新闻列表
     *
     * @param newsTable 新闻
     * @return 新闻
     */
    @Override
    public List<NewsTable> selectNewsTableList(NewsTable newsTable) {
        newsTable.setType(0);
        return newsTableMapper.selectNewsVoList(newsTable);
    }

    @Override
    public List<NewsTypeVo> selectNewsTypeList() {
        return newsTableMapper.selectAllNewsType();
    }

    /**
     * 新增新闻
     *
     * @param newsTable 新闻
     * @return 结果
     */
    @Override
    public int insertNewsTable(NewsTable newsTable) {
        newsTable.setCreateTime(DateUtils.getNowDate());
        return newsTableMapper.insertNewsTable(newsTable);
    }

    /**
     * 修改新闻
     *
     * @param newsTable 新闻
     * @return 结果
     */
    @Override
    public int updateNewsTable(NewsTable newsTable) {
        newsTable.setUpdateTime(DateUtils.getNowDate());
        return newsTableMapper.updateNewsTable(newsTable);
    }

    /**
     * 批量删除新闻
     *
     * @param ids 需要删除的新闻主键
     * @return 结果
     */
    @Override
    public int deleteNewsTableByIds(Long[] ids) {
        return newsTableMapper.deleteNewsTableByIds(ids);
    }

    /**
     * 删除新闻信息
     *
     * @param id 新闻主键
     * @return 结果
     */
    @Override
    public int deleteNewsTableById(Long id) {
        return newsTableMapper.deleteNewsTableById(id);
    }

    @Override
    public NewsTypeVo selectNewsListByType(Long typeId) {
        SysDictData sysDictData = sysDictDataMapper.selectDictDataById(typeId);
        NewsTypeVo newsTypeVo = new NewsTypeVo();
        newsTypeVo.setLabel(sysDictData.getDictLabel());
        newsTypeVo.setValue(Long.valueOf(sysDictData.getDictValue()));
        return newsTypeVo;
    }

    @Override
    public int deleteNewsTypeByIds(Long... ids) {
        return sysDictDataMapper.deleteDictDataByIds(ids);
    }

    @Override
    public int updateNewsType(NewsTypeVo newsTypeVo) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictCode(newsTypeVo.getValue());
        sysDictData.setDictLabel(newsTypeVo.getLabel());
        return sysDictDataMapper.updateDictData(sysDictData);
    }

    @Override
    public int insertNewsType(NewsTypeVo newsTypeVo) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictLabel(newsTypeVo.getLabel());
        sysDictData.setCreateTime(DateUtils.getNowDate());
        return sysDictDataMapper.insertDictData(sysDictData);
    }


}
